
----------	file:		wjxtdAutoPro.lua
----------	author:	kirimaru
----------	email:	holyiii@yahoo.com
----------	date:		11/05/2010

function Player:AnswerQuestion(nAnswer)
	me.AnswerQestion(nAnswer);
end

function Player:ShopExit()
	me.ShopExit();
end
function Player:DoRecipe(nRecipeId)
	me.DoRecipe(nRecipeId);
end
function Player:RepairAllEquip()
	UiNotify:OnNotify(UiNotify.emUIEVENT_REPAIREXALL_SEND);
end
function Player:RepairEquip(nIndex, nRepairType)
	local tbRepairList = { };
	table.insert(tbRepairList, nIndex);
	me.RepairEquipment(nRepairType, #tbRepairList, tbRepairList);
end
function Player:UnlockBox(nPassword)
	local nPos = 100000;
	local nPwd = 1;
	local nr1 = MathRandom(63);
	local nr2 = MathRandom(63);
	local nr3 = MathRandom(65535) / 65536;
	local nNameId = KLib.String2Id(tostring(me.GetNpc().dwId));
	while (nPos > 0) do
		local nKey = math.floor(nPassword / nPos);
		nPwd = nPwd * 10 + (nKey + math.floor(nNameId / nPos) % 10) % 10;
		nPassword = nPassword - nPos * nKey;
		nPos = math.floor(nPos / 10);
	end
	
	local nPsw = (nPwd % 1000000) * 64 + 32 + nr2 * 67108864;
	local nDivValue = nPsw / nr3;
	if math.abs(nDivValue * nr3 - nPsw) < 1 then
		me.CallServerScript{ "AccountCmd", Account.UNLOCK, nDivValue, nr3 };	
	else
		me.CallServerScript{ "AccountCmd", Account.UNLOCK, nPsw };			
	end	
end

function Player:InputNum(nNum)
	me.CallServerScript({"DlgCmd", "InputNum", nNum});
end

function Player:CanMake(nRecipeId)
	local tbRecipeList = me.GetRecipeList();
	if(tbRecipeList ~= nil) then
		for _, nId in ipairs(tbRecipeList) do
			if(tonumber(nRecipeId) == tonumber(nId)) then
				return 1;
			end
		end
	end
	return 0;
end

function Player:GetTodayPrestige()
	local nPrestige = KGblTask.SCGetDbTaskInt(DBTASD_EVENT_PRESIGE_RESULT);
	return nPrestige;
end

function Player:JingHuoBuy(nType, nCount)
	me.CallServerScript({ "JingHuoBuy", nType, nCount});
end

function Player:ApplyBillList()
	me.CallServerScript({ "JbExchangeCmd", "ApplyBillList" });
end

function Player:ApplyPlayerBillInfo()
	me.CallServerScript({ "JbExchangeCmd", "ApplyPlayerBillInfo" });	
end

function Player:ApplyCancelBill()
	local tbPlayerBill = me.GetPlayerBillInfo();
	if ((tbPlayerBill == nil) or (tbPlayerBill and tbPlayerBill.nIndex == 0)) then
		return;
	end
	me.CallServerScript({ "JbExchangeCmd", "ApplyCancelBill", tbPlayerBill.nIndex, tbPlayerBill.btType });
end

function Player:ApplyGetCashMoney(nAmount)
	local nRet = me.CallServerScript({ "JbExchangeCmd", "ApplyGetCashMoney", nAmount });
	if(nRet == nil) then
		return 0;
	end
	me.CallServerScript({ "JbExchangeCmd", "ApplyPlayerBillInfo" });
	return 1;
end

function Player:ApplyAddBill(nPrice, nCount, nType)
	local nRet = me.CallServerScript({ "JbExchangeCmd", "ApplyAddBill", nPrice, nCount, nType });
	if(nRet == nil) then
		return 0;
	end
	me.CallServerScript({ "JbExchangeCmd", "ApplyBillList" });
	me.CallServerScript({ "JbExchangeCmd", "ApplyPlayerBillInfo" });	
	return 1;
end

function Player:GetBillCash()
	local tbPlayerBill = me.GetPlayerBillInfo();
	if(tbPlayerBill ~= nil) then
		return tbPlayerBill.nCashMoney;
	end
	return -1;
end

function Player:GetBillCoin()
	local tbPlayerBill = me.GetPlayerBillInfo();
	if(tbPlayerBill ~= nil) then
		return tbPlayerBill.nCoinCount;
	end
	return -1;
end

function Player:EndTalk()
	me.EndTalk();
end

function Player:IbShop_GetWareList(nWareZone, nWareType, nPageNo)
	if (nWareZone < IbShop.GOLD_SECTION or nWareZone > IbShop.BINDGOLD_SECTION or
		nWareType <= 0 or nPageNo <= 0) then
		return;
	end
	local nFirstIndex = (nPageNo - 1) * IbShop.MAX_SHOW_NUMBER;
	return IbShop:GetWareList(nWareZone, nWareType, nFirstIndex, IbShop.MAX_SHOW_NUMBER);
end

function Player:GetPreWareId(nWareZone, nWareType, nPageNo)
	if (nWareZone < IbShop.GOLD_SECTION or nWareZone > IbShop.BINDGOLD_SECTION or
		nWareType <= 0 or nPageNo <= 0) then
		return 0;
	end
	if (nPageNo == 1) then
		return 0;
	else
		local tbPreWareList = self:IbShop_GetWareList(nWareZone, nWareType, nPageNo - 1);
		if (tbPreWareList and Lib:CountTB(tbPreWareList) ~= 0) then
			return tbPreWareList[Lib:CountTB(tbPreWareList)];
		else
			return 0;
		end
	end
end

function Player:IbShop_Apply(nWareZone, nWareType, nPageNo)
	if (nWareZone < IbShop.GOLD_SECTION or nWareZone > IbShop.BINDGOLD_SECTION or
		nWareType <= 0 or nPageNo <= 0) then
		return;
	end
	local nPreWareId = self:GetPreWareId(nWareZone, nWareType, nPageNo);
	me.IbShop_Apply(nWareZone, nWareType, nPreWareId, IbShop.MAX_SHOW_NUMBER);
end

function Player:IbShop_Open()
	me.IbShop_Open();
end

function Player:IbShop_Close()
	me.IbShop_Close();
end

function Player:IbCart_AddWare(nWareId, nSection)
	me.IbCart_AddWare(nWareId, nSection);
end

function Player:IbCart_SetWareCount(nWareId, nCount)
	me.IbCart_SetWareCount(nWareId, nCount);
end

function Player:IbCart_Commit(nCurrencyType)
	me.IbCart_Commit(nCurrencyType - 1);
end

function Player:IbCart_Clear()
	me.IbCart_Clear();
end

function Player:ApplyRepositoryInfo()
	me.CallServerScript({"PlayerCmd", "ApplyRepositoryInfo", me.nId, 0});
end

function wjxtdAutoPro:ClearMsgInfoTable()
	Ui.tbLogic.tbMsgInfo:Clear()
end

function LinkTask:ActiveLinkTask()
	local nTaskType		= self:GetTask(LinkTask.TSK_TASKTYPE);  
	local tbTask 		= Task:GetPlayerTask(me).tbTasks[nTaskType];
	if (tbTask ~= nil) then
		return 1;
	end
	return 0;
end

function LinkTask:SelectLinkTaskAward()
	local tbAward = me.GetChainTaskAward();
	local tbLinkTaskCfg = wjxtdAutoPro.tbLinkTaskCfg;
	if(tbAward ~= nil) then
		local nCancel = self:GetTask(LinkTask.TSK_CANCELNUM);
		for _, szAward in ipairs(tbLinkTaskCfg.tbAwardList) do
			for nIndex, tbItem in pairs(tbAward.tbOpt) do
				if(tbItem.szType == szAward) then
					if((tbItem.szType == "linktask_cancel") and (nCancel < tbLinkTaskCfg.nLinkTaskCancelMax)) then
						return nIndex;
					elseif((tbItem.szType == "item") and (tbItem.varValue[4] >= tbLinkTaskCfg.nAwardItemLevel)) then
						return nIndex;
					elseif((tbItem.szType == "bindmoney") and(tbItem.varValue >= tbLinkTaskCfg.nAwardBindMoneyLevel)) then
						return nIndex;
					elseif((tbItem.szType == "exp") and (tbItem.varValue >= tbLinkTaskCfg.nAwardExpLevel)) then
						return nIndex;
					end
				end
			end
		end
		
		for _, szAward in ipairs(tbLinkTaskCfg.tbAwardList) do
			for nIndex, tbItem in pairs(tbAward.tbOpt) do
				if(tbItem.szType == szAward) then
					return nIndex;
				end
			end
		end
	else
		return 0;
	end
	
	return 1;
end

function Task:SendAccept(nTaskId, nReferId, bAccept)
	KTask.SendAccept(me, nTaskId, nReferId, bAccept);
end

function Task:SendGeneralAward(nIndex)
	KTask.SendGeneralAward(me, nIndex);
end

function Task:GetCurStep(nTaskId)
	local tbPlayerTask = Task:GetPlayerTask(me);
	local tbTask = tbPlayerTask.tbTasks[nTaskId];
	return tbTask.nCurStep;
end

function wjxtdAutoPro:OnUiNotify(nEvent, ...)
	if(nEvent == UiNotify.emCOREEVENT_GET_CURE) then
		me.SendClientCmdRevive(2);
	end
end

function UiNotify:OnNotify(nEvent, ...)
	if self.tbCallback[nEvent] then
		for _, tb in ipairs(self.tbCallback[nEvent]) do
			local szUiGroup = tb.tbSelf.UIGROUP;
			tb.fnProc(tb.tbSelf, unpack(arg));
		end
	end
	if(wjxtdAutoPro.OnUiNotify ~= nil) then
		wjxtdAutoPro:OnUiNotify(nEvent, unpack(arg));
	end
end